package com.kye.express.business.jd.goods_detail.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.jd.goods_detail.model.JDAddressModel;
import com.kye.express.databinding.ItemJdAddressSelectionBinding;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/01/2018
 * @updated : 29/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDAddressSelectionViewBinder extends ItemViewBinder<JDAddressModel, JDAddressSelectionViewBinder.ViewHolder> {

    private OnAddressItemListener onItemListener;

    public JDAddressSelectionViewBinder(OnAddressItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_address_selection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDAddressModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdAddressSelectionBinding binding;
        private JDAddressModel item;
        private JDAddressModel preItem;
        private List<JDAddressModel> addressModels;
        private View view;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            binding = DataBindingUtil.bind(itemView);
            addressModels = (List<JDAddressModel>) getAdapter().getItems();
            RxView.clicks(binding.addressItem)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (item.isSelected()) {
                                return;
                            }
//                            item.setSelected(!item.isSelected());
                            view.post(new Runnable() {
                                @Override
                                public void run() {
                                    Observable.fromIterable(getAdapter().getItems())
                                            .map(new Function<Object, JDAddressModel>() {
                                                @Override
                                                public JDAddressModel apply(Object o) throws Exception {
                                                    return (JDAddressModel) o;
                                                }
                                            })
                                            .filter(new Predicate<JDAddressModel>() {
                                                @Override
                                                public boolean test(JDAddressModel jdAddressModel) throws Exception {
                                                    return jdAddressModel.isSelected();
                                                }
                                            })
                                            .take(1)
                                            .doOnNext(new Consumer<JDAddressModel>() {
                                                @Override
                                                public void accept(JDAddressModel jdAddressModel) throws Exception {
                                                    preItem = jdAddressModel;
                                                }
                                            })
                                            .flatMap(new Function<JDAddressModel, ObservableSource<Object>>() {
                                                @Override
                                                public ObservableSource<Object> apply(JDAddressModel jdAddressModel) throws Exception {
                                                    return Observable.fromIterable(getAdapter().getItems());
                                                }
                                            })
                                            .map(new Function<Object, JDAddressModel>() {
                                                @Override
                                                public JDAddressModel apply(Object o) throws Exception {
                                                    return (JDAddressModel) o;
                                                }
                                            })
                                            .doOnNext(new Consumer<JDAddressModel>() {
                                                @Override
                                                public void accept(JDAddressModel jdAddressModel) throws Exception {
                                                        jdAddressModel.setSelected(jdAddressModel.equals(item));
                                                }
                                            })
                                            .subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Consumer<JDAddressModel>() {
                                                @Override
                                                public void accept(JDAddressModel jdAddressModel) throws Exception {
                                                    getAdapter().notifyDataSetChanged();
                                                    view.post(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            if (onItemListener != null) {
                                                                onItemListener.onAddressItemClicked(preItem, item, addressModels);
                                                            }
                                                        }
                                                    });
                                                }
                                            });
                                }
                            });
                        }
                    });
        }

        public void bindData(JDAddressModel item) {
            this.item = item;
            binding.setData(item);
        }
    }

    public interface OnAddressItemListener {
        void onAddressItemClicked(JDAddressModel preSelected, JDAddressModel selected, List<JDAddressModel> addressList);
    }
}
